
const laypage = layui.laypage
let query = {
  page: 1,	//是	int	页码值
  limit: 10,//是	int	每页显示多少条数据

}


//右侧广告
function getAll() {
  axios.get('http://124.223.14.236:8060/index/index').then((res) => {
    // console.log(res)

    let newArr = res.data.data.sideBanner.map((item) => {
      return `
            <a href="${item.advimglink}" rel="nofollow" target="_blank" title="${item.advimgdesc}">
            <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${item.advimgsrc}" alt="" >
          </a>
            `
    })
    $('.textwidget').html(newArr.join(''))
  })
}
getAll()


function getPic() {
  axios.get('http://124.223.14.236:8060/index/index').then((res) => {
    console.log(res)

    let newPic = res.data.data.banner.map((item) => {
      return `
            <div class="swiper-slide">
            <dic class="focusmo">
            <ul>
              <li class="large">
              <a target="_blank" href="${item.advimglink}">
                <img class="thumb" src="http://124.223.14.236:8060/${item.advimgsrc}">
                <h4>${item.advimgdesc}</h4>
              </a>
            </li>
            </ul>
            </div>
            </div>
            `
    })
    $('.swiper-wrapper').html(newPic.join(''))
  })
}
getPic()




//热门推荐
function getList() {
  axios.get('http://124.223.14.236:8060/index/hot').then((res) => {
    console.log(res)

    let newList = res.data.data.list.map((item) => {
      return `
            
           
              <li class="item" data-id="${item.id}"><a target="_blank" href=""><img src="http://124.223.14.236:8060/${item.pic}"class="thumb" />${item.title}</a></li>
           
            
            `
    })
    $('.sticky').html(newList.join(''))
  })
}
getList()



// 点击跳转打开新页面
$('.sticky').on('click', 'li', function () {
  const id = $(this).attr('data-id')
  localStorage.setItem('index-id', id)

})



function getHot() {
  axios.get('http://124.223.14.236:8060/index/new', { params: query }).then((res) => {

    let newHot = res.data.data.list.data.map((item) => {
      return `
            <article class="excerpt excerpt-one" data-id="${item.id}">
            <header>
              <a class="cat label label-important" href="">享·优惠<i class="label-arrow"></i></a>
              <h2>
                <a target="_blank" href=""  title="阿里云拼团推荐服务器">${item.title}</a>
              </h2>
            </header>
            <p class="text-muted time">
              <a href="">${item.author}</a> 发布于 ${item.create_data}
            </p>
            <p class="focus">
              <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
                <span  class="item"><span class="thumb-span"><img  src="http://124.223.14.236:8060/${item.pic}" class="thumb" /></span></span></a>
              </p>
            <p class="note">${item.description}</p>
            <p class="text-muted views">
              <span class="post-views">阅读(${item.click})</span>
              <span class="post-comments">评论(${item.istop})</span>
              <a href="javascript:;" class="post-like">
                <i class="fa fa-thumbs-up"></i>赞 (<span>${item.lovenum}</span>)
              </a>
             </p>   
             </article>
            `

    })
    $('#article').html(newHot.join(''))
    renderPage(res.data.data.list.count)

  })
}
getHot()

// 保存到本底存储用show页面的使用
$('#article').on('click', '.excerpt-one', function () {
  const id = $(this).attr('data-id')
  localStorage.setItem('index-id', id)
  location.href = './show.html'
})

//右侧热门文章
function getTitle() {
  axios.get('http://124.223.14.236:8060/index/recommend').then((res) => {
    // console.log(res)

    let newTitle = res.data.data.list.map((item) => {
      return `
            <li>
              <a target="_blank" href="">
                <span class="thumbnail">
                  <img src="http://124.223.14.236:8060/${item.pic}" class="thumb" />
                </span>
                <span class="text">${item.title}</span>
                <span class="text-muted post-views">阅读(${item.click})</span>
              </a>
            </li>
            `
    })
    $('.items-02').html(newTitle.join(''))
  })
}
getTitle()

//右侧热门标签
function getTag() {
  axios.get('http://124.223.14.236:8060/index/index').then((res) => {
    console.log(res)

    let newTag = res.data.data.allTag.map((item) => {
      return `
            <li><a title="" href="">${item.tagname}</a></li>
            `
    })
    $('.widget_tags_inner').html(newTag.join(''))
  })
}
getTag()


//友情链接
function getAllLink() {
  axios.get('http://124.223.14.236:8060/index/index').then((res) => {
    console.log(res)

    let newAllLink = res.data.data.allLink.map((item) => {
      return `
           
            <li>
            <a href="${item.url}"><img src="http://124.223.14.236:8060/${item.img}" alt=""><p>${item.des}</p></a>
          </li>
         
            `
    })
    $('.widget.widget_comments ul').html(newAllLink.join(''))
  })
}
getAllLink()



// 实行分页功能
function renderPage(total) {
  laypage.render({
    elem: 'page-box', //注意，这里的 test1 是 ID，不用加 # 号
    count: total, //数据总数，从服务端得到
    limit: query.limit,  // 当前第几页（会决定第几个按钮是高亮）
    limits:[1,5,10],
    curr: query.page,  // 每页显示的条数
    layout: ['count', 'prev', 'page', 'next', 'limit','skip'],
    jump: function (obj, first) {
      // console.log(obj)
      //obj包含了当前分页的所有参数，比如：
      console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
      console.log(obj.limit); //得到每页显示的条数
      query.page = obj.curr
      query.limit = obj.limit
      // 完成切换分页效果 ==> 核心就是发ajax请求v
      //首次不执行
      if (!first) {
        getHot()
        // console.log(this)
      }
    }
  })
}
getHot()





